//
//  SinaHomeViewController.swift
//  WeiBo
//
//  Created by Apple on 15/12/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

let SinaHomeReuserIdentifer = "SinaHomeReuserIdentifer"

class SinaHomeViewController: SinaBaseTableViewController {

    // 创建一个 ViewModel的实例
    lazy var viewModel: SinaStatusListViewModel = SinaStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !isLogin {
            
            visitor?.setupInfo("关注一些人，回这里看看有什么惊喜", image: nil)

        } else {
            
            // 获取首页数据
//            getData()
            
            //设置重用标示
            tableView.registerClass(SinaHomeTableViewCell.self, forCellReuseIdentifier: SinaHomeReuserIdentifer)
            
            //预估高度
            tableView.estimatedRowHeight = 200
            //自动计算高度
            tableView.rowHeight = UITableViewAutomaticDimension
            
            //下拉刷新
            refreshControl = SinaRefreshControl()
            
            refreshControl?.addTarget(self, action: "reloadData", forControlEvents: UIControlEvents.ValueChanged)
        }
    
    }
    
    //下拉刷新UI提示
    func showCount(count: Int){
        
        let labelHeight: CGFloat = 44
        //创建一个视图
        let label = UILabel()
        label.frame = CGRectMake(0, 64 - labelHeight, kUIScreenWidth, labelHeight)
        label.backgroundColor = UIColor.orangeColor()
        
        if count == 0 {
            label.text = "暂无更新"
        }else{
            label.text = "更新了\(count)条微博"
        }
        
        label.textAlignment = NSTextAlignment.Center
        
        //第二步:添加进入
        navigationController?.view.insertSubview(label, belowSubview: navigationController!.navigationBar)
        //第三步:动画
        UIView.animateWithDuration(2, animations: { () -> Void in
            label.transform = CGAffineTransformMakeTranslation(0, labelHeight)
            }) { (_) -> Void in
                UIView.animateWithDuration(1.5, animations: { () -> Void in
                    //恢复到原来的状态
                    label.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        //动画完成之后移除
                        label.removeFromSuperview()
                })
        }
    }
    
    @objc private func reloadData(){
        
        getData()
        
        printLog("下拉刷新了")
    }
    
    private func getData(){
        
        SVProgressHUD.show()
        
//        viewModel.getData({ () -> () in
//            
//            //成功的回调,刷新UI
//            self.tableView.reloadData()
//            
//            SVProgressHUD.dismiss()
//            
//            }) { () -> () in
//                //失败的回调
//            SVProgressHUD.dismiss()
//        
//        
//        }
        
        let since_id = viewModel.list.first?.model?.id ?? 0
        
        viewModel.getData(since_id, success: { (count) -> () in
            
            //成功的回调,刷新UI
            self.tableView.reloadData()
            //刷新完成
            self.refreshControl?.endRefreshing()
            
            self.showCount(count)

                SVProgressHUD.dismiss()
            
            }) { () -> () in
                
                //刷新完成
                self.refreshControl?.endRefreshing()
                
                SVProgressHUD.dismiss()
        }
        
    }
    
    

      

}

extension SinaHomeViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.list.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(SinaHomeReuserIdentifer, forIndexPath: indexPath) as! SinaHomeTableViewCell
        
        
        // 测试数据
        cell.statusViewModel = viewModel.list[indexPath.row]
//        cell.textLabel?.text = viewModel.list[indexPath.row].user?.screen_name
        
        return cell
    }
    
}
